function HandleContainerReceipt(gui, item, options) {
  this._gui = gui;
  this._item = item;
  this._options = options;
  
  this._orderTrackId = (options && options.track) ? options.track : [];
  
  this._createElements();
};

HandleContainerReceipt.prototype._createElements = function() {
  var _self = this;
  
  for (var j = 0, jl = this._orderTrackId.length; j < jl; j++) {
    
    var order = DlmanageUtils.order(this._item.orders, this._orderTrackId[j]);
    var gui = this._gui.list[j];
    
    
    gui.orderType.appendChild(document.createTextNode(order.type));
    gui.date.appendChild(document.createTextNode(SimpleDate.format(order.date)));

    var total = 0, stage = 0;
    for (var i = 0, il = order.payments.length; i < il; i++) {
      var p = order.payments[i];
      if (p.cancelled == 1) {
        continue;
      }
      total += parseFloat(p.amount, 10);
      stage = Math.max(stage, p.stage);
    }
	
	if ('{{$smarty.const.BRANCH|escape:javascript}}' == 'BNE' || '{{$smarty.const.BRANCH|escape:javascript}}' == 'XTRAO') {
      var tolPrice = 0; price = 0; gst = 0;
      tolPrice = (order.price * parseFloat(1 + parseFloat('{{$smarty.const.GST|escape:javascript}}'), 10)).toFixed(2);
      price = (tolPrice / parseFloat(1 + parseFloat('{{$smarty.const.TRUE_GST|escape:javascript}}'), 10)).toFixed(2);
      gst = (tolPrice - price).toFixed(2);
      gui.price1.appendChild(document.createTextNode('$' + price));
      gui.gst.appendChild(document.createTextNode('$' + gst));
      gui.price.appendChild(document.createTextNode((order.price > 0) ? '$' + tolPrice : '$0.00'));
      gui.received.appendChild(document.createTextNode('$' + total.toFixed(2)));
      gui.anticipated.appendChild(document.createTextNode('$' + ((order.price * parseFloat(1 + parseFloat('{{$smarty.const.GST|escape:javascript}}'), 10)) - total).toFixed(2)));
    } else {
      gui.price.appendChild(document.createTextNode((order.price > 0) ? '$' + (order.price * parseFloat(1 + parseFloat('{{$smarty.const.GST|escape:javascript}}'), 10)).toFixed(2) + ' (Inc. ' + '{{$smarty.const.TAX|escape:javascript}}' + ')' : '$0.00'));
      gui.received.appendChild(document.createTextNode('$' + total.toFixed(2)));
      gui.anticipated.appendChild(document.createTextNode('$' + ((order.price * parseFloat(1 + parseFloat('{{$smarty.const.GST|escape:javascript}}'), 10)) - total).toFixed(2)));
    }
	
    //gui.price.appendChild(document.createTextNode((order.price > 0) ? '$' + (order.price * parseFloat(1 + parseFloat('{{$smarty.const.GST|escape:javascript}}'), 10)).toFixed(2) + ' (Inc. ' + '{{$smarty.const.TAX|escape:javascript}}' + ')' : '$0.00'));
    //gui.received.appendChild(document.createTextNode('$' + total.toFixed(2)));
    //gui.anticipated.appendChild(document.createTextNode('$' + ((order.price * parseFloat(1 + parseFloat('{{$smarty.const.GST|escape:javascript}}'), 10)) - total).toFixed(2)));
	
    
    var rules = DlmanageUtils.payment(order.type);
    if (rules[stage]) {
      gui.due.appendChild(document.createTextNode((((parseFloat(1 + parseFloat('{{$smarty.const.GST|escape:javascript}}'), 10) * order.price * rules[stage].rate - total).toFixed(2) > 0) ? '$' + (parseFloat(1 + parseFloat('{{$smarty.const.GST|escape:javascript}}'), 10) * order.price * rules[stage].rate - total).toFixed(2) + ' (Inc. ' + '{{$smarty.const.TAX|escape:javascript}}' + ')' : '$0.00')));
    } else {
      gui.due.appendChild(document.createTextNode((((parseFloat(1 + parseFloat('{{$smarty.const.GST|escape:javascript}}'), 10) * order.price - total).toFixed(2) > 0) ? '$' + (parseFloat(1 + parseFloat('{{$smarty.const.GST|escape:javascript}}'), 10) * order.price - total).toFixed(2) + ' (Inc. ' + '{{$smarty.const.TAX|escape:javascript}}' + ')' : '$0.00')));
    }
    
    if (rules[0]) {
      gui.pay1.rate.appendChild(document.createTextNode((((parseFloat(1 + parseFloat('{{$smarty.const.GST|escape:javascript}}'), 10) * order.price * rules[0].rate) - total).toFixed(2) > 0) ? '$' + ((parseFloat(1 + parseFloat('{{$smarty.const.GST|escape:javascript}}'), 10) * order.price * rules[0].perc)).toFixed(2) + ' (Inc. ' + '{{$smarty.const.TAX|escape:javascript}}' + ')' : '$0.00'));
      this._pay(gui.pay1, order, 1, rules[0]);
    } else {
      gui.pay1.container.style.display = 'none';
    }
    if (rules[1]) {
      gui.pay2.rate.appendChild(document.createTextNode((((parseFloat(1 + parseFloat('{{$smarty.const.GST|escape:javascript}}'), 10) * order.price * rules[1].rate) - total).toFixed(2) > 0) ? '$' + ((parseFloat(1 + parseFloat('{{$smarty.const.GST|escape:javascript}}'), 10) * order.price * rules[1].perc)).toFixed(2) + ' (Inc. ' + '{{$smarty.const.TAX|escape:javascript}}' + ')' : '$0.00'));
      this._pay(gui.pay2, order, 2, rules[1]);
    } else {
      gui.pay2.container.style.display = 'none';
    }
    if (rules[2]) {
      gui.pay3.rate.appendChild(document.createTextNode((((parseFloat(1 + parseFloat('{{$smarty.const.GST|escape:javascript}}'), 10) * order.price * rules[2].rate) - total).toFixed(2) > 0) ? '$' + ((parseFloat(1 + parseFloat('{{$smarty.const.GST|escape:javascript}}'), 10) * order.price * rules[2].perc)).toFixed(2) + ' (Inc. ' + '{{$smarty.const.TAX|escape:javascript}}' + ')' : '$0.00'));
      this._pay(gui.pay3, order, 3, rules[2]);
    } else {
      gui.pay3.container.style.display = 'none';
    }
    if (rules[3]) {
      gui.pay4.rate.appendChild(document.createTextNode((((parseFloat(1 + parseFloat('{{$smarty.const.GST|escape:javascript}}'), 10) * order.price * rules[3].rate) - total).toFixed(2) > 0) ? '$' + ((parseFloat(1 + parseFloat('{{$smarty.const.GST|escape:javascript}}'), 10) * order.price * rules[3].perc)).toFixed(2) + ' (Inc. ' + '{{$smarty.const.TAX|escape:javascript}}' + ')' : '$0.00'));
      this._pay(gui.pay4, order, 4, rules[3]);
    } else {
      gui.pay4.container.style.display = 'none';
    }
  }
  window.print();
};
  
HandleContainerReceipt.prototype._pay = function(gui, order, stage, rule) {
  var table, tr, td, _self = this;

  gui.rate.appendChild(document.createTextNode(' ' + rule.title));
  
  table = document.createElement('table');
  table.cellPadding = 0;
  table.cellSpacing = 0;
  gui.payment.appendChild(table);
  
  var total = 0;
  for (var i = 0, il = order.payments.length; i < il; i++) {
    var p = order.payments[i];
    if (p.stage != stage || p.cancelled == 1) {
      continue;
    }
    
    total += parseFloat(p.amount, 10);
    tr = table.insertRow(-1);
    td = tr.insertCell(-1);
    td.style.height = '15px';
    td.style.verticalAlign = 'middle';
    td.align = 'left';
    td.appendChild(document.createTextNode('$' + p.amount + ' '));
    td = tr.insertCell(-1);
    td.style.padding = '0px 4px';
    td.appendChild(document.createTextNode('paid on'));
    td = tr.insertCell(-1);
    
    td.appendChild(document.createTextNode(' ' + SimpleDate.format(p.date)));
  }
  
  gui.payment.appendChild(document.createTextNode((total == 0) ? 'Pay ' + rule.comment.toLowerCase() : ''));
  
};